home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / UTILITY / DO1002.ARJ / READ.SCR < prev    next >
Text File  |  1991-12-28  |  5KB  |  155 lines

  1. .pg wi full clr cy
  2.     COMMAND NAME»gray«: »%t«ReadFromFile»ye«
  3.  
  4.     /READ {handle} {delimiter} {variable} {variable} . . .
  5. /cw
  6.     The »%t«ReadFromFile»#« command reads lines from »ye«ASCII TEXT»#« files.
  7.     The file must have been opened with the »%t«/OPEN»#« command in read mode.
  8.  
  9.     The lines are expected to be in the format:
  10. »gr«
  11.     {field}»cy«{delimiter}»gr«{field}»cy«{delimiter}»gy« . . .
  12. »#«
  13.     Up to »wh«17 fields»#« (»cy«variables»#«) can be read from a file line.
  14.     The fields are placed into the named variables in the order
  15.     they are parsed from the record.  »ye«If there are more fields
  16.     than there are variables, the remaining fields will be
  17.     ignored.»#«
  18.  
  19.     The command jumps to the following labels on the indicated
  20.     conditions:
  21.  
  22.     »wh«{READERROR}»#«     Any error opening or reading the file.
  23.     »wh«{EOF}»#«           When end of file is reached. This is the
  24.                     normal process if you are reading an entire
  25.                     file using a loop.
  26.  
  27.     All variables are »ye«TEXT»#« variables.  The entire line read is
  28.     stored in the »ye«TEXT»#« variable »cy«FILEBUFFER»#«.  Each time a line is
  29.     read, »cy«FILEBUFFER»#« is overlaid.  Therefore if you wish to keep
  30.     a particular record, you must move it to another variable.
  31. .pg clr
  32. /GOSUB DispCode
  33. »go 1 24 ye«You might have this code.»#«
  34. /SET Blank " "
  35. .pg -24
  36. /GOSUB DispCode
  37. .go 1 5
  38. %blank(L60)
  39.     The repeat loop causes the program to read the file until
  40.     »cy«VAR1»#« equals the string stored in »cy«ANSWER»#«.  This could be used
  41.     to look up some parameters for a particular person, for
  42.     example.%blank(L40)
  43. %blank(L60)
  44. .pg -24
  45. /GOSUB DispCode
  46. .go 1 9
  47. %blank(L60)
  48.     If »cy«VAR1»#« never equals »cy«ANSWER»#« then when the end of file is
  49.     reached, the system will jump to the label »ye«:EOF»#« and set »cy«VAR2»#«
  50.     and »cy«VAR3»#« to "»gr«NOT FOUND»#«".%blank(L40)
  51. %blank(L60)
  52. .pg -24
  53. /GOSUB DispCode
  54. .go 1 6
  55. %blank(L60)
  56.     If there is some error reading the file, then the script will
  57.     jump to the label "»re«:READERROR»#«", display a message and exit.
  58. %blank(L60)
  59. .pg -24 clr
  60.  
  61. Let's set up a file to read from called »ye«READ.TMP»#«.  We'll open
  62. it an write some records to it using the »%t«/OPEN»#«, »%t«/WRITE»#«
  63. and »%t«/CLOSE»#« commands.
  64.  
  65. /OPEN 1 READ.TMP W
  66. /WRITE 1 "One,Field 1.1,Field 1.2,Field 1.3"
  67. /WRITE 1 "Two,Field 2.1,Field 2.2,Field 2.3"
  68. /WRITE 1 "Three,Field 3.1,Field 3.2,Field 3.3"
  69. /WRITE 1 "Four,Field 4.1,Field 4.2,Field 4.3"
  70. /WRITE 1 "Five,Field 5.1,Field 5.2,Field 5.3"
  71. /CLOSE 1
  72.  
  73. Press »bo«<enter>»#« to list the file, press »bo«<esc>»#« to exit »ye«SHOW.COM»#«
  74. when you are done . . .
  75. /PAGE
  76. /LIST READ.TMP
  77. .clr
  78.  
  79. Now we'll find one of the records and read in it's field. We'll use
  80. a loop like this to do the job:
  81. »wh«
  82.     /OPEN 1 READ.TMP R
  83.     /REPEAT
  84.     »%t«/READ»wh« 1 , Key Field1 Field2 Field3
  85.       Record: %%FileRecord
  86.     /PAGE -24                 »cy«<- To give you time to see the record»wh«
  87.     /UNTIL %Key EQ %Target                  »cy«press »bo«<enter>»cy« when you have finished
  88.                                  examining the record.
  89. »#«
  90. .pg
  91. /OPEN 1 READ.TMP R
  92. First, we'll search for Record "»gr«Two»#«".
  93.  
  94. |/SET Target TWO
  95.  
  96. /GOSUB FindRecord
  97.  
  98. As you can see, the case of the Key field does not matter to the
  99. match.
  100. .pg clr
  101. Now, let's look for a key that isn't there, "Six".
  102.  
  103. |/SET Target SIX
  104.  
  105. /GOSUB FindRecord
  106.  
  107. When the loop hit the end-of-file, »%t«DO»#« branched to "»ye«:EOF»#«" and the
  108. line above was printed.  Notice that »%t«DO»#« does »re«NOT»#« close the file
  109. unless you explicity use the "»%t«/CLOSE»#«" command.
  110.  
  111. If we had issued the »%t«/CLOSE»#« command between searches, »%t«DO»#« would have
  112. closed the file after the first search.
  113.  
  114. We could then have opened it with the »%t«/OPEN»#« command and the
  115. search would have started from the »ye«first record»#« in the file.
  116.  
  117. /GOTO END
  118.  
  119. :FindRecord
  120. /REPEAT
  121. /READ 1 , Key Field1 Field2 Field3
  122. »ma«Record»#«: »wh«%FileBuffer»#«
  123. /PAGE -24
  124. /UNTIL %Key EQ %Target
  125.  
  126. »gr«Record Found»cy«:»wh« %Key »gr«-»ye« %Field1»gr« - »ye«%Field2 »gr«- »ye«%Field3»#«
  127. /RETURN
  128. :EOF
  129. :READERROR
  130.  
  131. »wh«Record »+re«NOT»wh« Found for »gr«%Target»#«
  132. /RETURN
  133. :DispCode
  134. .clr wh
  135.     /OPEN 1 MYFILE.TXT R
  136.     /REPEAT
  137.     »%t«/READ»wh« 1 "," Var1 Var2 Var3
  138.     /UNTIL Var1 EQ %%Answer
  139.     /GOTO FOUND
  140.     :EOF
  141.     /SET Var2 "NOT FOUND"
  142.     /SET Var3 "NOT FOUND"
  143.     /GOTO FOUND
  144.     :READERROR
  145.         Unable to Open or Read file!  Aborting!
  146.     /GOTO END
  147.     :FOUND
  148.         Your parameters are %%Var2, %%Var3
  149.     :END
  150.     /CLOSE 1»#«
  151. /RETURN
  152. :END
  153. /CLOSE 1
  154. /ENDEXEC CLEAR
  155.